package com.lin.manager.pay.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lin.manager.pay.model.AliPay;
import com.lin.manager.pay.model.dto.PayDto;
import lombok.extern.slf4j.Slf4j;

/**
 * @作者: 林江
 * @创建时间: 2024/4/21
 * @功能:
 */
@Slf4j
public class AliPayService implements Pay {
    private final String name = "apliPay";
    private AliPay aliPay;
    private AlipayClient alipayClient;

    public AliPayService(AliPay aliPay) {
        this.aliPay = aliPay;
        init();
    }

    // 初始化客户端
    private void init() {
        if (this.aliPay != null) {
            this.alipayClient = new DefaultAlipayClient(
                    this.aliPay.getUrl(), // 支付网关
                    this.aliPay.getAppid(), // 签约的appid
                    this.aliPay.getPriveteKey(), // 私钥
                    this.aliPay.getFormat(), // 格式
                    this.aliPay.getCharset(), // 支付编码
                    this.aliPay.getPublicKey(), // 公钥
                    this.aliPay.getSignType() // 加密方式
            );
        }
    }

    public String getName() {
        return name;
    }

    @Override
    public void pay(PayDto dto) {
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(this.aliPay.getReturnUrl());
        alipayRequest.setNotifyUrl(this.aliPay.getNotifyUrl());

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = dto.getOutTradeNo();
        //付款金额，必填
        String total_amount = dto.getTotalAmount().doubleValue() + "";
        //订单名称，必填
        String subject = dto.getTitle();
        //商品描述，可空
        String body = dto.getDescript();

        //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"10m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        try {
            String result = alipayClient.pageExecute(alipayRequest).getBody();

        } catch (AlipayApiException e) {
            log.error("支付宝支付异常：{}", e);
            throw new RuntimeException("支付失败");
        }

    }

    @Override
    public void query() {

    }

    @Override
    public void close() {

    }

    @Override
    public void refund() {

    }

    @Override
    public void refundQuery() {

    }
//
//    /**
//     * 处理结果
//     *
//     * @param result
//     * @throws IOException
//     */
//    private void out(String result) throws IOException {
//        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletResponse response = attr.getResponse();
//        response.setContentType("text/html;charset=" + this.aliPay.getCharset());
//        response.getWriter().write(result);//直接将完整的表单html输出到页面
//        response.getWriter().flush();
//        response.getWriter().close();
//    }
}
